var Q = require("q"),
    mongoose = require('mongoose'),
    entityCollection = require('../../../Base/server/controllers/EntityCollection');

exports.deleteEntity = function(req, res) {
    var fullname = req.body.FullName,
        data = JSON.parse(req.body.Data)

    var deferred = Q.defer();

    var entityName = '';
    var entityRecords = [];

    if (fullname == "UFIDA.U9.BC.BaseData.BarCode") {
        entityName = "BarcodeMaster";
    } else if (fullname === "UFIDA.U9.CBO.SCM.Bin.Bin") {
        entityName = "Bin";
    } else if (fullname == "UFIDA.U9.CBO.SCM.Supplier.Supplier") {
        entityName = "Supplier";
    } else if (fullname == "UFIDA.U9.CBO.SCM.Customer.Customer") {
        entityName = "Customer";
    } else if (fullname == "UFIDA.U9.CBO.SCM.Warehouse.Warehouse") {
        entityName = "StorageLocation";
    }

    var entity = entityCollection.getEntity(entityName);
    var delError = { IsSuccess: true, ErrorMsg: "" };
    data.forEach(function(item) {
        entity.deleteByCondition({
            ErpID: "" + item
        }, function(


            err) {
            if (err) {
                delError.IsSuccess = false;
                delError.ErrorMsg += err.message;
            }
        });
    });
    if (delError) {
        delError.ErrorMsg = "同步数据失败：" + delError.Error
        res.json(delError);
        res.end();
        deferred.reject(delError.ErrorMsg);
    }

    return deferred.promise;
};